package com.yangguang.huahui.ui.activitys;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.hyphenate.easeui.widget.EaseAlertDialog;
import com.yangguang.huahui.R;
import com.yangguang.huahui.baseadapter.BaseQuickAdapter;
import com.yangguang.huahui.baseadapter.BaseViewHolder;
import com.yangguang.huahui.entity.Contact;
import com.yangguang.huahui.entity.DataInfo;
import com.yangguang.huahui.entity.HttpResult;
import com.yangguang.huahui.mvp.model.CommonModel;
import com.yangguang.huahui.mvp.model.NewsModel;
import com.yangguang.huahui.mvp.presenter.BlacklistPresenter;
import com.yangguang.huahui.mvp.view.ContactView;
import com.yangguang.huahui.rx.RxManager;
import com.yangguang.huahui.rx.RxSubscriber;
import com.yangguang.huahui.utils.ImageLoader;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class BlacklistActivity extends BaseMvpActivity<ContactView, BlacklistPresenter> implements ContactView {
    @BindView(R.id.tv_title)
    TextView tv_title;
    @BindView(R.id.mRecyclerView)
    RecyclerView mRecyclerView;
    BlacklistAdapter mAdapter;
    List<Contact.ListBean> datas = new ArrayList<>();

    @OnClick({R.id.iv_back})
    void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_back:
                finish();
                break;
        }
    }

    @Override
    protected int initLayoutId() {
        return R.layout.activity_blacklist;
    }

    @Override
    protected void initView() {
        tv_title.setText(getString(R.string.my_blacklist));
        mRecyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        mAdapter = new BlacklistAdapter(R.layout.contact_list_item, datas);
        //设置加载动画
        mAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        //将适配器添加到RecyclerView
        mRecyclerView.setAdapter(mAdapter);
    }

    @Override
    protected void initData() {

    }

    @Override
    public void onSuccess(Contact contact) {
        if (contact.getList() == null) {
            mAdapter.notifyDataSetChanged();
            return;
        }
        datas = contact.getList();
        mAdapter.setNewData(datas);
    }

    @Override
    public void onError() {

    }

    @Override
    protected BlacklistPresenter initPresenter() {
        return new BlacklistPresenter();
    }

    @Override
    protected void fetchData() {
        mPresenter.getBlacklist(DataInfo.TOKEN);
    }

    private class BlacklistAdapter extends BaseQuickAdapter<Contact.ListBean> {

        public BlacklistAdapter(int layoutResId, List<Contact.ListBean> data) {
            super(layoutResId, data);
        }

        @Override
        protected void convert(final BaseViewHolder helper, final Contact.ListBean item) {
            ImageLoader.loadCircle(mContext, item.getAvatar(), (ImageView) helper.getView(R.id.iv_head));
            helper.setText(R.id.tv_name, item.getNickname());
            helper.getConvertView().setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View view) {

                    new EaseAlertDialog(mContext, com.hyphenate.easeui.R.string.delete, com.hyphenate.easeui.R.string.move_out_backlist, null, new EaseAlertDialog.AlertDialogUser() {
                        @Override
                        public void onResult(boolean confirmed, Bundle bundle) {
                            if (!confirmed) {
                                return;
                            }
                            RxManager.getInstance().doSubscribe(new CommonModel().remove_black(item.getId()), new RxSubscriber<HttpResult>(false) {
                                @Override
                                protected void _onNext(HttpResult httpResult) {
                                    if (httpResult.getStatus() == 200) {
                                        mData.remove(helper.getPosition());
                                        notifyDataSetChanged();
                                    }
                                }

                                @Override
                                protected void _onError() {

                                }
                            });
                        }
                    }, true).show();
                    return false;
                }
            });
        }
    }
}
